import requests
import hashlib
import warnings

import yaml


def post(url, data: str = ..., header: dict = ..., encoding="utf-8") -> requests.Response:
    """
    post中文参数的函数
    :param encoding: 编码,默认utf-8
    :param url: post函数目标链接
    :param data: post数据
    :param header: post请求头
    :return: requests返回的对象
    """
    try:
        data = data.encode(encoding)
        res = requests.post(url, data=data, headers=header, verify=False)
        return res
    except BaseException as e:
        print("post请求错误，错误原因：%s" % e)
        raise e


def get(url, data: str = ..., header: dict = ..., encoding="utf-8") -> requests.Response:
    """
    get中文参数的函数
    :param encoding: 编码,默认utf-8
    :param url: get函数目标链接
    :param data: get数据
    :param header: get请求头
    :return: requests返回的对象
    """
    try:
        data = data.encode(encoding)
        res = requests.post(url, data=data, headers=header, verify=False)
        return res
    except BaseException as e:
        print("get请求错误，错误原因：%s" % e)
        raise e


def string_hash(string: str, encoding: str = "md5") -> str:
    hashDict = {"md5": hashlib.md5, "sha1": hashlib.sha1, "sha2": hashlib.sha256,
                "sha384": hashlib.sha384, "sha512": hashlib.sha512, "sha224": hashlib.sha224}
    encodeFunc = hashDict[encoding]
    return encodeFunc(string.encode()).hexdigest()


def adv_hash(string: str, encoding: str = "md5", salt: str = "", mode: str = "word", number: int = 1) -> str | list:
    if mode == "word" and number == 1:
        return string_hash(string + salt, encoding)
    else:
        if mode == "group":
            if salt != "":
                warnings.warn("In group mode salt has no effect", category=SyntaxWarning)
            groupList = []
            for i in string:
                s = i
                for j in range(number):
                    s = string_hash(s, encoding=encoding)
                groupList.append(s)
            return groupList
        else:
            s = string + salt
            for i in range(number):
                s = string_hash(s, encoding=encoding)
            return s


def yaml_config(path, encoding="utf-8"):
    file = open(path, "r", encoding=encoding)
    data = file.read()
    file.close()
    data = yaml.safe_load(data)
    return data
